<?php

/**
 * HTML2PDF (http://www.html2pdf.fr) version library used.
 *
 * @var string
 *
 */
define("HTML2PDF_VERSION", "4.02");

include_once(dirname(__FILE__).DIRECTORY_SEPARATOR."html2pdf_v".HTML2PDF_VERSION.DIRECTORY_SEPARATOR."html2pdf.class.php");
include (dirname(__FILE__) . DIRECTORY_SEPARATOR . "phpQuery". DIRECTORY_SEPARATOR . "phpQuery-onefile.php");

/**
 * Extension to HTML2PDF class used to parse Harliane's variables in tag :
 * &lt;HFIELD name="my.variable" /&gt;
 *
 * @author herve.guenot
 * @version $Id: CHtml2Pdf.php,v 1.2 2011-06-17 04:43:20 herve Exp $
 * @copyright 2011
 */
class CHtml2Pdf extends HTML2PDF
{

	/**
	 * Create a new Html2Pdf instance to convert HTML files in PDF.
	 *
	 * @param string $orientation Page orientation (default to P(ortrait)).
	 * @param string $format Page format (default to A4).
	 * @param string $langue File language (default to fr).
	 * @param boolean $unicode Generates a unicode file or not (true by default).
	 * @param string $encoding Files encoding (default to UTF-8).
	 * @param array $marges Array of 4 integer defining document margins (no margin by default).
	 */
	public function __construct($orientation = 'P', $format = 'A4', $langue='fr', $unicode=true, $encoding='ISO-8859-1', $marges = array(0,0,0,0))
	{
		parent::__construct($orientation, $format, $langue, $unicode, $encoding, $marges);
	}

	public function writeHtml($html, $context, $debugVue = false)
	{
		$this->nloop = 0;
		$xml = phpQuery::newDocument($html);
		$loop = new CallbackBody('$i,$v,$pdf,$context', '$pdf->loop($context, pq($v)); ',new CallbackParam(), new CallbackParam(),$this, $context);
		$func = new CallbackBody('$i,$v,$pdf,$context', '$pdf->replace($context, pq($v)); ',new CallbackParam(), new CallbackParam(),$this, $context);

		phpQuery::each($xml->find('foreach')->not($xml->find('foreach foreach')), $loop);
		phpQuery::each($xml->find('hfield'), $func);
		
		parent::writeHTML($xml, $debugVue);
	}

	public function replace($context, $pqNode)
	{
			$value = $context->valueOf($pqNode->attr("name"));
			
			$df = $pqNode->attr("dateformat");
			if (isset($df))
			{
				if ($value instanceof Date) {
					$value = self::convert_date($value->toMysqlDateTimeString(), 'yyyy-MM-dd HH:mm:ss', $df);
					//$value = $value->toMysqlDateTimeString();
				} else {
					// formatting a date/time
					$value = self::convert_date($value, 'yyyy-MM-dd', $df);
				}
			}

			$p = $pqNode->parent();
			$node = pq("<span>".$value."</span>", $pqNode);
			$p->get(0)->replaceChild($node->get(0), $pqNode->get(0));
	}

	public function loop($context, $pqNode)
	{
			$p = $pqNode->parent();

			$a = $context->valueOf($pqNode->attr("on"));

			if (isset($a))
			{
				foreach ($a as $node)
				{
					$c = clone $context;
					$c->put($pqNode->attr("name"), $node);

					$xml = phpQuery::newDocument($pqNode->html());
					
					$loop = new CallbackBody('$i,$v,$pdf,$context', '$pdf->loop($context, pq($v)); ', new CallbackParam(), new CallbackParam(),$this, $c);
					$func = new CallbackBody('$i,$v,$pdf,$context', '$pdf->replace($context, pq($v)); ', new CallbackParam(), new CallbackParam(),$this, $c);
					
					phpQuery::each($xml->find('foreach')->not($xml->find('foreach foreach')), $loop);
					phpQuery::each($xml->find('hfield'), $func);
					
					$pqNode->before($xml);
				}
			}
			$pqNode->remove();
	}

	public function display($context, $pqNode)
	{
		//echo "<pre>";var_dump("=display == ".$pqNode->attr("on")); echo "</pre>";
	}

	protected static function convert_date($date, $source_format, $display_format) {
		$d = CDateTimeParser::parse($date, $source_format);
		return Yii::app()->dateFormatter->format($display_format, $d);
	}
	
}
